#include "DashboardUI_ViewRecords.h"
#include "ui_dashboard_viewrecords.h"

#include "Authentication.h"

DashboardUI_ViewRecords::DashboardUI_ViewRecords(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::DashboardUI_ViewRecords)
{
    ui->setupUi(this);

    loadRecords();
}

DashboardUI_ViewRecords::~DashboardUI_ViewRecords()
{
    delete ui;
}

void DashboardUI_ViewRecords::loadRecords()
{
    Authentication *auth = Authentication::getInstance();
    User *user = auth->getUser();

    if (user->getUserType() == User::PATIENT)
        recordDB.loadRecords(user->getId());
    else
    {
        User *patient = auth->getPatient();

        if (patient != nullptr)
            recordDB.loadRecords(patient->getId());
    }

    ui->tableView->reset();
    ui->tableView->setModel(recordDB.getModel());
}

void DashboardUI_ViewRecords::reload()
{
    loadRecords();
}
